package com.yupi.referral.controller;

import com.yupi.referral.common.BaseResponse;
import com.yupi.referral.common.ErrorCode;
import com.yupi.referral.common.ResultUtils;
import com.yupi.referral.exception.BusinessException;
import com.yupi.referral.model.entity.Referral;
import com.yupi.referral.model.entity.User;
import com.yupi.referral.service.ReferralAnswerService;
import com.yupi.referral.service.ReferralService;
import com.yupi.referral.service.UserService;
import com.zretc.security.entity.LoginUser;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import io.minio.errors.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.List;

/**
 * 内推响应接口
 *
 * @author <a href="https://github.com/liyupi">程序员鱼皮</a>
 * @from <a href="https://yupi.icu">编程导航知识星球</a>
 */
@RestController
@RequestMapping("/referral_answer")
@Slf4j
public class ReferralAnswerController {

    @Resource
    private ReferralAnswerService referralAnswerService;

    @Resource
    private ReferralService referralService;

//    @Resource
//    private UserService userService;

    /**
     * 内推响应
     *
     * @param referralIdStr
     * @param request
     * @return 提交记录id
     */
    @PostMapping("/answerReferral")
    public BaseResponse<Long> doReferralAnswer(@RequestParam("referralId") String referralIdStr, @RequestParam("file") MultipartFile file,
            HttpServletRequest request) throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
        long referralId = Long.parseLong(referralIdStr);
        if (referralIdStr == null || referralId<= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        // 登录才能
//        final User loginUser = userService.getLoginUser(request);
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        final LoginUser user = (LoginUser) authentication.getPrincipal();

        long result = referralAnswerService.doReferralAnswer(referralId, user, file);
        return ResultUtils.success(result);
    }



    /**
     * 根据referralId返回对应的所有address
     *
     * @param referralId
     * @return
     */
    @GetMapping("/getAddresses")
    public List<String> getAddressListByReferralId( Long referralId, HttpServletRequest request) {
        if (referralId <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        List<String> referralAnswers = referralAnswerService.getAddressListByReferralId(referralId);
        if (referralAnswers == null || referralAnswers.isEmpty()) {
            throw new BusinessException(ErrorCode.NOT_FOUND_ERROR);
        }
        // 仅本人或管理员可编辑
        Referral oldReferral = referralService.getById(referralId);
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        LoginUser user = (LoginUser) authentication.getPrincipal();
        if (!oldReferral.getUserId().equals(user.getUser().getUserId())) {
            throw new BusinessException(ErrorCode.NO_AUTH_ERROR);
        }
        return ResultUtils.success(referralAnswerService.getAddressListByReferralId(referralId)).getData();
    }

}
